//
//  SettingViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/5.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "SettingViewController.h"

#import "SettingTableViewCell.h"
#import "Personal_InformationViewController.h"
#import "AccountAndSafetyViewController.h"
#import "MessageNoticeViewController.h"
#import "ConcealViewController.h"
#import "Setting_GeneralViewController.h"
#import "Setting_InviteColleagueViewController.h"
#import "Setting_AboutUsViewController.h"

@interface SettingViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"设置";
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 35, 15)];
    lbl_return.text = @"我";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 60, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[SettingTableViewCell class] forCellReuseIdentifier:@"cell_cols"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 2)
    {
        return 3;
    }
    else
    {
        return 1;
    }
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 5)
    {
        return 100;
    }
    else
    {
        return 50;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 5)
    {
        UITableViewCell * cell = [[UITableViewCell alloc] init];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        UIButton * btn_Exit = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_Exit.frame = CGRectMake(10, 3, SCREEN_WIDTH - 20, 45);
        btn_Exit.layer.cornerRadius = 5;
        btn_Exit.backgroundColor = [UIColor colorWithRed:244/255.0 green:92/255.0 blue:96/255.0 alpha:1.0];
        [btn_Exit setTitle:@"退出登录" forState:(UIControlStateNormal)];
        [btn_Exit setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        btn_Exit.titleLabel.font = [UIFont systemFontOfSize:17];
        [cell addSubview:btn_Exit];
        [btn_Exit addTarget:self action:@selector(btn_ExitAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        return cell;
    }
    else
    {
        SettingTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell_cols" forIndexPath:indexPath];
        
        if(indexPath.section == 0)
        {
            cell.name_cols.text = @"个人资料";
        }
        else if(indexPath.section == 1)
        {
            cell.name_cols.text = @"账号与安全";
        }
        else if(indexPath.section == 3)
        {
            cell.name_cols.text = @"邀请同事";
        }
        else if(indexPath.section == 4)
        {
            cell.name_cols.text = @"关于XXXX";
        }
        else if(indexPath.section == 2)
        {
            switch (indexPath.row)
            {
                case 0:
                {
                    cell.name_cols.text = @"新消息通知";
                }
                    break;
                case 1:
                {
                    cell.name_cols.text = @"隐私";
                }
                    break;
                case 2:
                {
                    cell.name_cols.text = @"常规";
                }
                    break;
                default:
                    break;
            }
        }
        
        return cell;
    }
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 0)
    {
//        NSLog(@"点击进入个人资料");
        
        Personal_InformationViewController * personal_InformationViewController = [[Personal_InformationViewController alloc] init];
        [self showViewController:personal_InformationViewController sender:nil];
    }
    else if(indexPath.section == 1)
    {
//        NSLog(@"点击进入账号与安全");
        AccountAndSafetyViewController * accountAndSafetyViewController = [[AccountAndSafetyViewController alloc] init];
        [self showViewController:accountAndSafetyViewController sender:nil];
    }
    else if(indexPath.section == 3)
    {
//        NSLog(@"点击进入邀请同事");
        Setting_InviteColleagueViewController * setting_InviteColleagueViewController = [[Setting_InviteColleagueViewController alloc] init];
        [self showViewController:setting_InviteColleagueViewController sender:nil];
    }
    else if(indexPath.section == 4)
    {
//        NSLog(@"点击进入关于XXXX");
        Setting_AboutUsViewController * setting_AboutUsViewController = [[Setting_AboutUsViewController alloc] init];
        [self showViewController:setting_AboutUsViewController sender:nil];
    }
    else if(indexPath.section == 2)
    {
        switch (indexPath.row)
        {
            case 0:
            {
//                NSLog(@"点击进入新消息通知");
                MessageNoticeViewController * messageNoticeViewController = [[MessageNoticeViewController alloc] init];
                [self showViewController:messageNoticeViewController sender:nil];
            }
                break;
            case 1:
            {
//                NSLog(@"点击进入隐私");
                ConcealViewController * concealViewController = [[ConcealViewController alloc] init];
                [self showViewController:concealViewController sender:nil];
            }
                break;
            case 2:
            {
//                NSLog(@"点击进入常规");
                Setting_GeneralViewController * setting_GeneralViewController = [[Setting_GeneralViewController alloc] init];
                [self showViewController:setting_GeneralViewController sender:nil];
            }
                break;
            default:
                break;
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc] init];
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

#pragma mark - 点击退出登录
- (void )btn_ExitAction:(UIButton *)sender
{
//    NSLog(@"点击退出登录");
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"" message:@"您确定退出此账号？" preferredStyle:(UIAlertControllerStyleAlert)];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    UIAlertAction * action_Cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action_Cancel];
    
    UIAlertAction * action_OK = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action_OK];
}

@end
